import 'package:flutter/material.dart';
import 'CalendarController.dart';
import 'CalendarUtil.dart';
import 'CalendarCarousel.dart';
import 'CalendarWeekday.dart';

class CalendarSliverDelegate extends SliverPersistentHeaderDelegate {

  final DateTime startTime;  // 启始日期
  final DateTime endTime;  // 结束日期
  final DateTime currentPageTime;  // 当前要显示的日期, 默认当前时间

  final CurrentPageDate currentPageDate;

  final CalendarWeekday weekdayHeader;
  final DayWidgetBuilder dayWidgetBuilder;
  final double childAspectRatio;
  final CalendarController calendarController;
  final Color? bgColor;

  CalendarSliverDelegate({
    required this.startTime,
    required this.endTime,
    required this.weekdayHeader,
    required this.currentPageDate,
    required this.dayWidgetBuilder,
    this.childAspectRatio = 1,
    required this.calendarController,
    required this.currentPageTime,
    this.bgColor
  });

  @override
  Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
    if(shrinkOffset > maxExtent - minExtent) {
      shrinkOffset = maxExtent - minExtent;
    }

    return Container(
      color: bgColor,
      child: CalendarCarousel(
        shrinkageHeight: shrinkOffset,
        startTime: startTime,
        endTime: endTime,
        currentPageTime: currentPageTime,
        calendarShow: CalendarShow.calendarShowMonth,
        weekdayHeader: weekdayHeader,
        dayWidgetBuilder:dayWidgetBuilder,
        calendarController: calendarController,
        currentPageDate: currentPageDate,
        childAspectRatio: childAspectRatio,
      ),
    );
  }


  @override
  double get maxExtent {
    double max = calendarController.getMaxHeight(childAspectRatio,weekdayHeader.firstDayOfWeek, currentPageTime);
    if(max <= 0) {
      return 300;
    }
    return max + weekdayHeader.height;
  }

  @override
  double get minExtent {
    double min = calendarController.getMinHeight(childAspectRatio,weekdayHeader.firstDayOfWeek, currentPageTime);
    return min + weekdayHeader.height;
  }

  @override
  bool shouldRebuild(CalendarSliverDelegate oldDelegate) {
    return maxExtent != oldDelegate.maxExtent ||
        minExtent != oldDelegate.minExtent ||
        currentPageTime != oldDelegate.currentPageTime;
  }
}

